from django.db import models

# Create your models here.
from utils.Base_model import Base


class Tag(models.Model):
    title = models.CharField('标签', max_length=24)

    class Meta:
        db_table = 'tb_tag'

    def __str__(self):
        return self.title


class Course(models.Model):
    FEE_CHOICE = (
        (1, '免费'),
        (2, '限免'),
        (3, 'Vip'),
    )
    STATUS_CHOICE = (
        (1, '已上线'),
        (2, '即将上线')
    )
    fee = models.IntegerField('收费类型', choices=FEE_CHOICE)
    tag = models.ForeignKey(Tag, on_delete=models.SET_NULL, null=True)
    teacher = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True)
    status = models.IntegerField('上线状态', choices=STATUS_CHOICE)
    pub_time = models.DateTimeField('发布时间', null=True)  # 最新排序
    study_num = models.IntegerField('学习数量', default=0)

    img = models.ImageField('图片', upload_to='course', null=True)
    title = models.CharField('课程名', max_length=64)
    desc = models.CharField('课程描述', max_length=256)
    attention_num = models.IntegerField('关注数量', default=0)

    price = models.DecimalField('价格', default=0, max_digits=10, decimal_places=2)

    class Meta:
        db_table = 'tb_course'

    def __str__(self):
        fee_dict = dict(self.FEE_CHOICE)
        # {1:'免费', 2:'限免', 3:'Vip'}
        return '%s-%s' % (self.title, fee_dict.get(self.fee))

    def count_length(self):
        # 计算课程总时长
        count = 0
        for chapter in self.chapter_set.all():
            for section in chapter.section_set.all():
                count += section.length
        return count


class Chapter(models.Model):
    title = models.CharField('章', max_length=64)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_chapter'

    def __str__(self):
        return '%s-%s' % (self.course.title, self.title)


class Section(models.Model):
    title = models.CharField('节', max_length=64)
    chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE)
    video = models.FileField('视频', upload_to='video')
    play_num = models.IntegerField('播放次数', default=0)
    length = models.IntegerField('视频时长(分)', default=0)

    class Meta:
        db_table = 'tb_section'

    def __str__(self):
        return '%s-%s-%s' % (self.chapter.course.title, self.chapter.title, self.title)


class Follow(Base):
    user = models.ForeignKey('user.User', on_delete=models.CASCADE)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_follow'


# 用户学习的课程及小节
class UserCourseSection(Base):
    user = models.ForeignKey('user.User', on_delete=models.CASCADE)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    sections = models.CharField('小节列表', max_length=512, null=True)

    class Meta:
        db_table = 'tb_user_course_section'

    def __str__(self):
        return '%s-%s-%s' % (self.user.username, self.course.title, self.sections)


# 课程评论表
class CourseComment(Base):
    user = models.ForeignKey('user.User', on_delete=models.CASCADE)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    content = models.TextField('评论内容')
    to = models.ForeignKey('self',null=True, verbose_name='回复谁', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_course_comment'
        ordering = ['c_time']

    def __str__(self):
        if self.to:
            return '%s-%s to %s' % (self.course.title, self.user.nick_name, self.to.user.nick_name)
        return '%s-%s' % (self.course.title, self.user.nick_name)
